<template>
  <el-dialog
    title="新增"
    :visible.sync="isShow"
    width="600px"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="姓名：" prop="username">
        <el-input v-model="form.username" placeholder="1-30字符" />
      </el-form-item>
      <el-form-item label="手机：" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间：" prop="formOfEmployment">
        <el-date-picker
          v-model="form.timeOfEntry"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式：" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号：" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="1-20字符" />
      </el-form-item>
      <el-form-item label="部门：" prop="departmentName">
        <!-- input中有两个事件：foucs事件和blur失去焦点事件 -->
        <div v-clickOutside="outsideFn">
          <el-input
            v-model="form.departmentName"
            placeholder="请选择"
            readonly
            @focus="focusEvent"
          />
          <!-- node-clcik节点事件点击能获取到该项目的对象 -->
          <el-tree
            v-if="treeShow"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间：" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" placeholder="转正时间" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button>取消</el-button>
        <el-button type="primary" @click="submmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employeesData from '@/api/constant/employees'
import { companyDepartment } from '@/api/departments'
import { sysUserAdd } from '@/api/employees'
export default {
  data() {
    return {
      treeShow: false,
      treeData: [],
      isShow: false,
      //  拿到聘用形式的数据
      hireType: employeesData.hireType,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', // string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', // string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 30, message: '请输入1-30个字符' }
        ],
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确手机号',
            trigger: 'change'
          }
        ],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 20, message: '请输入1-20个字符' }
        ],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 提交表单新增员工
    submmit() {
      this.$refs.form.validate(async(results) => {
        if (results) {
          const res = await sysUserAdd(this.form)
          //  提示新增成功
          this.$message.success(res.message)
          // 关闭弹框
          this.isShow = false
          // 刷新父级
          this.$emit('getData')
        }
      })
    },
    async focusEvent() {
      // 显示tree部分
      this.treeShow = true
      // 避免皆苦请求浪费没有数据再发送接口请求
      if (this.treeData.length === 0) {
        // 点击input框上时获取树形结构数据
        const res = await companyDepartment()
        console.log(res)
        // 数据处理
        this.treeData = this.getTree(res.data.depts, '')
      }
    },
    // 过滤数据使数据呈树形结构输出
    getTree(arr, str) {
      // 找出一级数据
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.getTree(arr, item.id)
          return true
        }
      })
    },
    nodeClick(data) {
      this.form.departmentName = data.name
      this.treeShow = false
    },
    // 关闭弹框清空表单验证和字段内容
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    },
    outsideFn() {
      // 关闭el-tree
      if (this.treeShow === false) return
      console.log('treeShow')
      this.treeShow = false
    }
  }
}
</script>

<style></style>
